package com.hobobe.tailor.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

import com.jcraft.jzlib.JZlib;
import com.jcraft.jzlib.ZInputStream;
import com.jcraft.jzlib.ZOutputStream;

public class ZlibUtil {
	private static final Logger log = Logger.getLogger(ZlibUtil.class);
	// 输入数据的最大长度
	private static final int MAXLENGTH = 102400;

	// 设置缓存大小
	private static final int BUFFERSIZE = 1024;

	@SuppressWarnings("deprecation")
	public static byte[] inflate(byte[] bContent) {
		if(bContent==null){
			return null;
		}
		byte[] data = new byte[MAXLENGTH];
		ByteArrayInputStream in=null;
		ZInputStream zIn=null;
		DataInputStream objIn =null;
		int len = 0;
		int count = 0;
		byte[] trueData=null;
		try {
			in = new ByteArrayInputStream(bContent);
			zIn = new ZInputStream(in);
			objIn = new DataInputStream(zIn);
			
			while ((count = objIn.read(data, len, len + BUFFERSIZE)) != -1) {
				len = len + count;
			}

			trueData = new byte[len];
			System.arraycopy(data, 0, trueData, 0, len);

			return trueData;

		} catch (IOException e) {
			e.printStackTrace();
			log.error("ZlibUtil.inflate(..) IOException",e);
		}finally{
			try {
				if(objIn!=null){
					objIn.close();
				}
				if(zIn!=null){
					zIn.close();
				}
				if(in!=null){
					in.close();
				}
			} catch (IOException e) {
				log.error("ZlibUtil.inflate(..) close IOException",e);
			}
		}
		return null;
	}

	@SuppressWarnings("deprecation")
	public static byte[] deflate(byte[] bContent) {
		if(bContent==null){
			return null;
		}
		byte[] data = null;
		ByteArrayOutputStream out=null;
		ZOutputStream zOut =null;
		DataOutputStream objOut=null;
		try {
			out = new ByteArrayOutputStream();
			zOut = new ZOutputStream(out,JZlib.Z_BEST_COMPRESSION); // 压缩级别,缺省为1级
			objOut = new DataOutputStream(zOut);
			objOut.write(bContent);
			objOut.flush();
			zOut.close();
			data = out.toByteArray();
		} catch (IOException e) {
			log.error("ZlibUtil.deflate(..) IOException",e);
		}finally{
			try {
				if(out!=null){
					out.close();
				}
			} catch (IOException e) {
				log.error("ZlibUtil.inflate(..) close IOException",e);
			}
		}
		return data;
	}
	public static void main(String[] args){
		String test="阿萨德鸡啊数据的阿萨德鸡啊数据的阿斯达是的加速度阿斯达简奥斯丁阿斯达简奥斯丁阿斯达就我最后一次测试asdhahsd成啊啊爱死爱死hhashd{'\bjn,mv  slkmdsmo4,as00p ms slb !@#$%^&*()_+?><\"':}";
		byte[] b = test.getBytes();
		System.out.println("数据大小："+b.length);
		System.out.println("压缩数据大小："+ZlibUtil.deflate(b).length);
		System.out.println("压缩数据后base64："+Base64.encodeBase64String(ZlibUtil.deflate(b)));
//		int[] ints = new int[test.length()];
//		int num=0;
//		for(char c:test.toCharArray()){
//			ints[num]=(int)c;
//			num++;
//		}
		System.out.println("ascii编码后数据："+Arrays.toString(b));
//		System.out.println("ascii编码后数据大小："+Arrays.toString(ints).getBytes().length);
//		
//		System.out.println("ascii编码后压缩数据大小："+ZlibUtil.deflate(Arrays.toString(ints).getBytes()).length);
		
		
//		System.out.println("ascii编码后压缩数据base64："+Base64.encodeBase64String(ZlibUtil.deflate(Arrays.toString(ints).getBytes())));
		
//		System.out.println(Base64.encodeBase64String(new String(ZlibUtil.deflate(b)).getBytes()));
//		System.out.println(new String(ZlibUtil.deflate(b)).getBytes().length);
//		System.out.println(Arrays.toString(ZlibUtil.deflate(b)));
//		System.out.println(Base64.encodeBase64String(ZlibUtil.deflate(b)));
//		System.out.println(Base64.encodeBase64String(ZlibUtil.deflate(b)).getBytes().length);
//		
//		
//		System.out.println(Arrays.toString(ZlibUtil.deflate(Arrays.toString(ints).getBytes())));
//		System.out.println(Base64.encodeBase64String(ZlibUtil.deflate(Arrays.toString(ints).getBytes())));
	}
}
